<?php

/**
 * 支付调用接口 
 */
class PayService {

    static $paySnPre = ""; //接口前缀

    function __construct() {
        if (C("ZFH_ON_LINE_MODE")) {
            self::$paySnPre = C("paySnPre");
        } else {
            $type = (IS_WIN) ? 1 : 0;
            $date = date('Ymd');
            self::$paySnPre = "PayTest{$date}_{$type}_";
        }
    }

    function insert($param) {
        /**
         * 1.判断交易号是否正确
         * 2.判断真实支付的金额是否正确
         * 3.插入支付记录 并且 支付记录与交易号关联
         * 4.返回信息
         */
        //插入支付表
        $param['pay_status'] = 0;
        $param['pay_time'] = 0;

        $pay_id = M('Pay')->data($param)->add();
        $pay_sn = self::$paySnPre . sprintf('%08d', $pay_id); //生成PaySn @todo 上线的时候需要改回来
        $saveData = array('pay_sn' => $pay_sn);

        M('Pay')->where("pay_id = '{$pay_id}'")->save($saveData);

        return $pay_id;
    }

    public function pay_service_get($pay_id) {
        $param = M('Pay')->where("pay_id = '{$pay_id}'")->find();
        if (!$param) {
            return reStatus(false, "未找到相关支付记录");
        }
        $pay_sn = $param['pay_sn'];
        $payCode = ucfirst($param['pay_name']);
        $payConfig = C("PayMode.{$payCode}");
        $payConfig['pay_id'] = $pay_id; //生成的支付ID
        $payConfig['pay_sn'] = $pay_sn; //生成的支付ID
        $payConfig['pay_price'] = format_int($param['price_cash']); //支付金额
        $payConfig['pay_desc'] = $pay_sn . "-" . $param['uid']; //备注
        $payConfig['return_url_hide'] = G::$Cache['siteurl'] . U("Pay/{$payCode}/confirm/?pay_id={$pay_id}&hide=1"); //隐藏的地址,及后台的回调地址
        $payConfig['return_url_show'] = G::$Cache['siteurl'] . U("Pay/{$payCode}/confirm/?pay_id={$pay_id}"); //显示的地址,及前台的回调地址


        import("@.Service.Extend.Pay.Pay");
        import("@.Service.Extend.Pay.{$payCode}.{$payCode}Pay");

        $className = $payCode . "Pay";
        $pay = new $className($payConfig);
        $payContent = $pay->pay_get();

        $data = array(
            "show" => 'HTML',
            'showData' => $payContent
        );
        return reStatus(true, "成功操作", $data);
    }

    public function pay_service_confirm($param) {
        $payCode = $param['payMode'];
        import("@.Service.Extend.Pay.Pay");
        import("@.Service.Extend.Pay.{$payCode}.{$payCode}Pay");
        $className = $payCode . "Pay";
        $payConfig = C("PayMode.{$payCode}");
        $pay = new $className($payConfig);
        $payContent = $pay->pay_confirm($param);
        return reStatus($payContent['status'], $payContent['msg'], $payContent['data']);
    }

    public function payPrice($param) {
        $payMod = M('Pay');
        $payData = $payMod->where(array('pay_sn' => $param['pay_sn']))->find();
        if (format_int($payData['price_cash']) == format_int($param['price'])) {
            //金额 = 真实支付金额
            $payMod->startTrans();
            if ($payData['pay_status'] == 1) {
                //如果支付了但是没有充值成功
                $where = array(
                    'pay_id' => $payData['pay_id'],
                    'real_cash' => $param['price'],
                    'pay_status' => 1
                );

                $saveData = array(
                    'pay_status' => 0,
                    'pay_time' => time()
                );

                $payMod->where($where)->save($saveData);
            }

            $where = array(
                'pay_id' => $payData['pay_id'],
                'price_cash' => $param['price'],
                'pay_status' => 0
            );

            $saveData = array(
                'pay_status' => 1,
                'pay_time' => time()
            );

            $flag = $payMod->where($where)->save($saveData);
            if ($flag) {
                $where = array(
                    'pay_id' => $payData['pay_id'],
                    'price_cash' => $param['price'],
                );
                $saveData = array(
                    'pay_status' => 2,
                    'pay_time' => time()
                );
                $flag = $payMod->where($where)->save($saveData);
                $param['price'] = $param['price'] / 100;
                $this->payLogs($payData['pay_id'], "支付成功", time(), 1);
                $payMod->commit();
                D("Pay")->doConfirm($payData['pay_id']);
            }
        } else {
            $flag = false;
            $this->payLogs($payData['pay_id'], "现金支付失败,支付金额不相等;", time(), -1);
        }
        if (!$flag) {
            return reStatus(false, "出现错误,请查看支付日志", $payData);
        } else {
            return reStatus(true, "现金部分支付成功!", $payData);
        }
    }

    public function payLogs($payID, $msg, $time, $status, $admin_status = 0) {
        $logs = array(
            'pay_id' => $payID,
            'msg' => $msg,
            'add_time' => $time,
            'is_suc' => $status,
            'admin_status' => $admin_status
        );
        return M('PayLogs')->data($logs)->add();
    }

}

?>
